package com.bnq.control.common.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PermissionUtils {


    public static final String READ_PERMISSION = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String WRITE_PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE;

    private static final String[] permissions_storage = new String[]{
            READ_PERMISSION, WRITE_PERMISSION
    };

    /**
     * 是否有存储权限
     *
     * @param context 上下文
     * @return
     */
    public static boolean hasStoragePermission(Context context) {
        if (BuildConfig.hasQ() || !BuildConfig.hashM()) {
            return true;
        } else {
            return hasPermission(context, WRITE_PERMISSION);
        }
    }

    /**
     * 是否有权限
     *
     * @param context    上下文
     * @param permission 权限名称
     * @return
     */
    public static boolean hasPermission(Context context, String permission) {
        if (!BuildConfig.hashM()) {
            return true;
        } else {
            return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
        }
    }

    /**
     * 申请读写存储权限(对于只申请读写存储权限的，使用此方法)
     */
    public static void requestStoragePermission(final Activity activity) {
        ActivityCompat.requestPermissions(activity, permissions_storage, 1);
    }

}
